<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>成长照片 | 小糯米一周岁生日</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">

    <!-- 配置Tailwind -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#FF6B9B',
                        secondary: '#6BCBFF',
                        accent: '#FFD66B',
                        dark: '#1A1A2E'
                    },
                    fontFamily: {
                        cute: ['"Comic Sans MS"', '"Bubblegum Sans"', 'cursive'],
                    }
                }
            }
        }
    </script>

    <style type="text/tailwindcss">
        @layer utilities {
            .photo-item {
                transition: all 0.3s ease;
                cursor: pointer;
            }
            .photo-item:hover {
                transform: scale(1.03);
            }
            .album-tab {
                transition: all 0.3s ease;
                cursor: pointer;
            }
            .album-tab.active {
                background-color: #FF6B9B;
                color: white;
            }
            .photo-modal {
                opacity: 0;
                pointer-events: none;
                transition: opacity 0.3s ease;
            }
            .photo-modal.active {
                opacity: 1;
                pointer-events: all;
            }
            .modal-content {
                transform: scale(0.9);
                transition: transform 0.3s ease;
            }
            .photo-modal.active .modal-content {
                transform: scale(1);
            }
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .photo-item {
            animation: fadeIn 0.5s ease forwards;
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen font-cute overflow-x-hidden">
    <!-- 背景音乐 -->
    <audio id="background-music" loop>
        <source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mpeg">
    </audio>
    <audio id="click-sound">
        <source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-4.mp3" type="audio/mpeg">
    </audio>

    <!-- 主内容 -->
    <div class="container mx-auto px-4 py-8 relative z-10">
        <!-- 导航 -->
        <nav class="flex justify-between items-center mb-10">
            <a href="index.html" class="text-gray-700 hover:text-primary transition-colors flex items-center">
                <i class="fa fa-home mr-2"></i> 首页
            </a>
            <h1 class="text-2xl md:text-3xl font-bold text-primary">成长照片集</h1>
            <div class="w-10"></div> <!-- 占位，保持导航平衡 -->
        </nav>

        <!-- 页面标题 -->
        <div class="text-center mb-10">
            <h2 class="text-[clamp(1.5rem,5vw,3rem)] font-bold text-primary mb-4">小糯米的成长瞬间</h2>
            <p class="text-gray-600 max-w-2xl mx-auto">记录从出生到一岁的珍贵回忆，每一张照片都是成长的足迹</p>
        </div>

        <!-- 相册分类标签 -->
        <div class="max-w-4xl mx-auto mb-10 overflow-x-auto pb-2">
            <div class="flex gap-2 min-w-max justify-center">
                <div class="album-tab active px-6 py-3 bg-white rounded-full shadow-md text-gray-700" data-album="all">全部照片</div>
                <div class="album-tab px-6 py-3 bg-white rounded-full shadow-md text-gray-700" data-album="newborn">新生儿时期</div>
                <div class="album-tab px-6 py-3 bg-white rounded-full shadow-md text-gray-700" data-album="3months">3个月</div>
                <div class="album-tab px-6 py-3 bg-white rounded-full shadow-md text-gray-700" data-album="6months">6个月</div>
                <div class="album-tab px-6 py-3 bg-white rounded-full shadow-md text-gray-700" data-album="9months">9个月</div>
                <div class="album-tab px-6 py-3 bg-white rounded-full shadow-md text-gray-700" data-album="1year">1岁啦</div>
            </div>
        </div>

        <!-- 照片网格 -->
        <div id="photos-container" class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 max-w-6xl mx-auto mb-16">
            <!-- 照片将通过JS动态生成 -->
        </div>

        <!-- 照片查看器模态框 -->
        <div id="photo-modal" class="photo-modal fixed inset-0 bg-black/90 z-50 flex items-center justify-center p-4">
            <div class="modal-content max-w-5xl w-full bg-white rounded-xl overflow-hidden">
                <div class="relative">
                    <button id="close-modal" class="absolute top-4 right-4 w-10 h-10 bg-black/50 text-white rounded-full flex items-center justify-center z-10 hover:bg-black/70 transition-colors">
                        <i class="fa fa-times"></i>
                    </button>
                    <button id="prev-photo" class="absolute left-4 top-1/2 transform -translate-y-1/2 w-10 h-10 bg-black/50 text-white rounded-full flex items-center justify-center z-10 hover:bg-black/70 transition-colors">
                        <i class="fa fa-arrow-left"></i>
                    </button>
                    <button id="next-photo" class="absolute right-4 top-1/2 transform -translate-y-1/2 w-10 h-10 bg-black/50 text-white rounded-full flex items-center justify-center z-10 hover:bg-black/70 transition-colors">
                        <i class="fa fa-arrow-right"></i>
                    </button>
                    <img id="modal-image" src="" alt="照片大图" class="w-full h-auto">
                </div>
                <div class="p-6">
                    <h3 id="modal-title" class="text-2xl font-bold text-primary mb-2"></h3>
                    <p id="modal-description" class="text-gray-600"></p>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 照片数据
        let photosData = [];
        let currentPhotoIndex = 0;

        // DOM加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 加载照片数据
            fetch('data/content.json')
                .then(response => {
                    if (!response.ok) throw new Error('数据加载失败');
                    return response.json();
                })
                .then(data => {
                    photosData = data.photos;

                    // 初始化页面
                    initGallery();
                    initModal();
                    initTabs();
                })
                .catch(error => {
                    console.error('初始化失败:', error);
                    alert('页面加载失败，请刷新重试');
                });

            // 播放背景音乐
            const music = document.getElementById('background-music');
            music.volume = 0.2;
            music.play().catch(e => {
                console.log('需要用户交互才能播放音乐:', e);
                document.addEventListener('click', () => {
                    music.play();
                }, { once: true });
            });
        });

        // 初始化相册
        function initGallery() {
            const container = document.getElementById('photos-container');
            container.innerHTML = '';

            // 创建照片项
            photosData.forEach((photo, index) => {
                const photoItem = document.createElement('div');
                photoItem.className = `photo-item rounded-xl overflow-hidden shadow-md bg-white ${photo.album}`;
                photoItem.dataset.index = index;
                photoItem.style.animationDelay = `${index * 0.05}s`;

                photoItem.innerHTML = `
                    <div class="relative aspect-square overflow-hidden">
                        <img src="${photo.thumbnail}" alt="${photo.title}" class="w-full h-full object-cover">
                        <div class="absolute inset-0 bg-primary/20 opacity-0 hover:opacity-100 transition-opacity flex items-center justify-center">
                            <i class="fa fa-search-plus text-white text-3xl"></i>
                        </div>
                    </div>
                    <div class="p-3">
                        <h4 class="font-bold text-gray-800 text-sm truncate">${photo.title}</h4>
                        <p class="text-gray-500 text-xs">${getAlbumName(photo.album)}</p>
                    </div>
                `;

                // 点击查看大图
                photoItem.addEventListener('click', function() {
                    currentPhotoIndex = parseInt(this.dataset.index);
                    openPhotoModal(currentPhotoIndex);
                    document.getElementById('click-sound').play();
                });

                container.appendChild(photoItem);
            });
        }

        // 初始化标签切换
        function initTabs() {
            const tabs = document.querySelectorAll('.album-tab');

            tabs.forEach(tab => {
                tab.addEventListener('click', function() {
                    // 移除所有标签的active类
                    tabs.forEach(t => t.classList.remove('active'));
                    // 给当前标签添加active类
                    this.classList.add('active');

                    const album = this.dataset.album;
                    const photos = document.querySelectorAll('.photo-item');

                    // 筛选照片
                    photos.forEach(photo => {
                        if (album === 'all' || photo.classList.contains(album)) {
                            photo.style.display = 'block';
                        } else {
                            photo.style.display = 'none';
                        }
                    });
                });
            });
        }

        // 初始化模态框
        function initModal() {
            // 关闭模态框
            document.getElementById('close-modal').addEventListener('click', closePhotoModal);

            // 上一张
            document.getElementById('prev-photo').addEventListener('click', function(e) {
                e.stopPropagation();
                currentPhotoIndex = (currentPhotoIndex - 1 + photosData.length) % photosData.length;
                updateModalContent(currentPhotoIndex);
                document.getElementById('click-sound').play();
            });

            // 下一张
            document.getElementById('next-photo').addEventListener('click', function(e) {
                e.stopPropagation();
                currentPhotoIndex = (currentPhotoIndex + 1) % photosData.length;
                updateModalContent(currentPhotoIndex);
                document.getElementById('click-sound').play();
            });

            // 点击模态框背景关闭
            document.getElementById('photo-modal').addEventListener('click', function(e) {
                if (e.target === this) {
                    closePhotoModal();
                }
            });

            // 键盘导航
            document.addEventListener('keydown', function(e) {
                if (!document.getElementById('photo-modal').classList.contains('active')) return;

                if (e.key === 'Escape') {
                    closePhotoModal();
                } else if (e.key === 'ArrowLeft') {
                    currentPhotoIndex = (currentPhotoIndex - 1 + photosData.length) % photosData.length;
                    updateModalContent(currentPhotoIndex);
                } else if (e.key === 'ArrowRight') {
                    currentPhotoIndex = (currentPhotoIndex + 1) % photosData.length;
                    updateModalContent(currentPhotoIndex);
                }
            });
        }

        // 打开照片模态框
        function openPhotoModal(index) {
            updateModalContent(index);
            document.getElementById('photo-modal').classList.add('active');
            document.body.style.overflow = 'hidden'; // 防止背景滚动
        }

        // 关闭照片模态框
        function closePhotoModal() {
            document.getElementById('photo-modal').classList.remove('active');
            document.body.style.overflow = ''; // 恢复滚动
        }

        // 更新模态框内容
        function updateModalContent(index) {
            const photo = photosData[index];
            document.getElementById('modal-image').src = photo.large;
            document.getElementById('modal-title').textContent = photo.title;
            document.getElementById('modal-description').textContent = photo.description;
        }

        // 获取相册名称
        function getAlbumName(albumKey) {
            const albumNames = {
                'newborn': '新生儿时期',
                '3months': '3个月',
                '6months': '6个月',
                '9months': '9个月',
                '1year': '1岁啦'
            };
            return albumNames[albumKey] || albumKey;
        }
    </script>
</body>
</html>